/**
 * Created by root on 2017-05-16.
 */
$(function(){
    var user = JSON.parse( sessionStorage.getItem("user") );
    if (!user) {
        window.location.href = "../html/login.html";
    } else {
        $.post(config.devUrl + "/cart/getShoppingCart.json",
            {
                userId: user.id,
                shoppingCartType: 0
            },
        function(result){
            if (result.status == config.success) {
                result.data.goodsJson =  JSON.parse( result.data.goodsJson );
                // 渲染页面
                var html =  template('shopCartScript',  result.data );
                $("#shopCartData").html(html);
                // 存session
                var goodsString = JSON.stringify(result);
                sessionStorage.setItem("goodsCart", goodsString);
            } else {
                alert(result.msg)
            }
        }, "json");
    }
    // 全选
    $("#allSelected").on('change', function(){
        if ($(this).attr("checked") == "checked") {
            $(".goodsInCart").attr("checked",'true');//全选
        } else {
            $(".goodsInCart").removeAttr("checked");//取消全选
        }
        $("#shopCartData .goodsInCart").change();
    });
    // 结算金额
    $("#shopCartData").on("change",".goodsInCart", function(){
        var money = 0.0;
        var isAll = true;
        $("#shopCartData tr").each(function(){
            var checkout = $(this).find("input[type='checkbox']");
            if (checkout.attr("checked")) {
                var price = $(this).find(".shopPrice span").html().substr(1);
                var number = $(this).find(".shopNumber input").val();
                money += (price * number);
            } else {
                isAll = false;
            }
        });
        if (isAll) {
            $("#allSelected").attr("checked",'checked');//全选
        } else {
            $("#allSelected").removeAttr("checked");//取消全选
        }
        $("#allMoney").html(money);
    });
    // 删除
    $("#shopCartData").on("click",".del", function(){
        var thisId = $(this).attr("data-id");
        var goods = JSON.parse(sessionStorage.getItem("goodsCart"));
        console.info(goods);
        for (var o in goods.data.goodsJson) {
            if( goods.data.goodsJson[o].id == thisId ) {
               (goods.data.goodsJson).splice(o,1);
            }
        }
        $.post(config.devUrl + "/cart/saveShoppingCart.json",
            {
                userId : goods.data.userId,
                shoppingCartType : 0,
                goodsJson : JSON.stringify(goods.data.goodsJson)
            },
            function(result) {
                if(result.status == config.success) {
                    window.location.reload();
                } else {
                    alert(result.msg);
                }
            }
        );
    });

    // 结算下单
    $("#jsBtn").on("click",function(){
        alert("缺少地址选择页面");
        /*$.post(config.devUrl + "/order/saveOrder.json",
            {
                userId: user.id,
                orderAmount: 0,
                orderGoodsJson:"",
                optionresult: "",
                addressId: "",
            },
            function(result){

            }
        );*/
    });
})
